{"componentChunkName":"component---src-templates-post-jsx","path":"/[BOJ]14503/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"f1c04e8a-df0a-5294-949f-5f3a483cbcb0","excerpt":"문제 로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어져 있다. 각각의 칸은 벽 또는 빈 칸이다. 청소기는 바라보는 방향이 있으며, 이 방향은 동, 서, 남, 북중 하나이다. 지도의 각 칸은 (r, c)로 나타낼 수…","html":"<h2>문제</h2>\n<p>로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오.</p>\n<p>로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어져 있다. 각각의 칸은 벽 또는 빈 칸이다. 청소기는 바라보는 방향이 있으며, 이 방향은 동, 서, 남, 북중 하나이다. 지도의 각 칸은 (r, c)로 나타낼 수 있고, r은 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로 부터 떨어진 칸의 개수이다.</p>\n<p>로봇 청소기는 다음과 같이 작동한다.</p>\n<ol>\n<li>현재 위치를 청소한다.</li>\n<li>현재 위치에서 현재 방향을 기준으로 왼쪽 방향부터 차례대로 인접한 칸을 탐색한다.\na. 왼쪽 방향에 아직 청소하지 않은 공간이 존재한다면, 그 방향으로 회전한 다음 한 칸을 전진하고 1번부터 진행한다.\nb. 왼쪽 방향에 청소할 공간이 없다면, 그 방향으로 회전하고 2번으로 돌아간다.\nc. 네 방향 모두 청소가 이미 되어있거나 벽인 경우에는, 바라보는 방향을 유지한 채로 한 칸 후진을 하고 2번으로 돌아간다.\nd. 네 방향 모두 청소가 이미 되어있거나 벽이면서, 뒤쪽 방향이 벽이라 후진도 할 수 없는 경우에는 작동을 멈춘다.</li>\n</ol>\n<p>로봇 청소기는 이미 청소되어있는 칸을 또 청소하지 않으며, 벽을 통과할 수 없다.</p>\n<h2>입, 출력</h2>\n<p>입력</p>\n<ul>\n<li>첫째 줄에 세로 크기 N과 가로 크기 M이 주어진다. (3 ≤ N, M ≤ 50)</li>\n<li>둘째 줄에 로봇 청소기가 있는 칸의 좌표 (r, c)와 바라보는 방향 d가 주어진다. d가 0인 경우에는 북쪽을, 1인 경우에는 동쪽을, 2인 경우에는 남쪽을, 3인 경우에는 서쪽을 바라보고 있는 것이다.</li>\n<li>셋째 줄부터 N개의 줄에 장소의 상태가 북쪽부터 남쪽 순서대로, 각 줄은 서쪽부터 동쪽 순서대로 주어진다. 빈 칸은 0, 벽은 1로 주어진다. 지도의 첫 행, 마지막 행, 첫 열, 마지막 열에 있는 모든 칸은 벽이다.</li>\n<li>로봇 청소기가 있는 칸의 상태는 항상 빈 칸이다.</li>\n</ul>\n<p>출력</p>\n<ul>\n<li>로봇 청소기가 청소하는 칸의 개수를 출력한다.</li>\n</ul>\n<h3>📍 <strong>Logic</strong></h3>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">class Robot {\n    int x;\n    int y;\n    int dir;\n\n    public Robot(int x, int y, int dir) {\n        this.x = x;\n        this.y = y;\n        this.dir = dir;\n    }\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>로봇 청소기의 위치를 저장하는 클래스</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">public int cleanRoom(int r, int c, int d) {\n    int cleanCount = 0;\n    queue.add(new Robot(r, c, d));\n\n    while(!queue.isEmpty()) {\n        Robot robot = queue.poll();\n\n        if (map[robot.x][robot.y] == 0) {\n            map[robot.x][robot.y] = 2;\n            cleanCount++;\n        }\n\n        if (!checkWallMethod(robot)) {\n            moveBack(robot);\n        }\n    }\n    return cleanCount;\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>문제에서 주어진 작동 로직을 구현한 메소드</li>\n<li>Queue를 이용한 BFS방식으로 청소할 영역을 탐색한다.</li>\n</ul>\n<h3>📄 <strong>CODE</strong></h3>\n<details>\n  <summary>코드 보기/접기💫</summary>\n    <div markdown=\"1\">\n<deckgo-highlight-code  terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">import java.io.*;\nimport java.util.*;\n\nclass Robot {\n    int x;\n    int y;\n    int dir;\n\n    public Robot(int x, int y, int dir) {\n        this.x = x;\n        this.y = y;\n        this.dir = dir;\n    }\n}\n\nclass Directions {\n    int x;\n    int y;\n\n    public Directions(int x, int y) {\n        this.x = x;\n        this.y = y;\n    }\n}\n\nclass Room {\n    int[][] map;\n    int N, M;\n    Queue&lt;Robot&gt; queue = new LinkedList&lt;&gt;();\n    Directions[] directions = {new Directions(-1, 0), new Directions(0, 1), new Directions(1, 0), new Directions(0, -1)};\n\n    public Room (int n, int m) {\n        N = n;\n        M = m;\n        map = new int[n][m];\n    }\n\n    public int cleanRoom(int r, int c, int d) {\n        int cleanCount = 0;\n        queue.add(new Robot(r, c, d));\n\n        while(!queue.isEmpty()) {\n            Robot robot = queue.poll();\n\n            if (map[robot.x][robot.y] == 0) {\n                map[robot.x][robot.y] = 2;\n                cleanCount++;\n            }\n\n            if (!checkWallMethod(robot)) {\n                moveBack(robot);\n            }\n        }\n        return cleanCount;\n    }\n\n    private void moveBack(Robot robot) {\n        int backDir = turnBack(robot.dir);\n        int backX = robot.x + directions[backDir].x;\n        int backY = robot.y + directions[backDir].y;\n\n        if (!checkBoundary(backX, backY)) {\n            if (map[backX][backY] != 1) {\n                queue.add(new Robot(backX, backY, robot.dir));\n            }\n        }\n    }\n\n    private boolean checkWallMethod(Robot robot) {\n        for (int i = 0; i &lt; 4; i++) {\n            int nextDir = turnLeft(robot.dir);\n            int nextX = robot.x + directions[nextDir].x;\n            int nextY = robot.y + directions[nextDir].y;\n\n            robot.dir = nextDir;\n\n            if (checkBoundary(nextX, nextY)) continue;\n\n            if (map[nextX][nextY] == 0) {\n                queue.add(new Robot(nextX, nextY, nextDir));\n                return true;\n            }\n        }\n        return false;\n    }\n\n    private boolean checkBoundary(int r, int c) {\n        return r &lt; 0 || r &gt;= N || c &lt; 0 || c &gt;= M;\n    }\n\n    private int turnLeft(int d) {\n        return (d + 3) % 4;\n    }\n\n    private int turnBack(int d) {\n        return (d + 2) % 4;\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) throws IOException {\n        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n        String[] line = br.readLine().split(&quot; &quot;);\n        int N = Integer.parseInt(line[0]);\n        int M = Integer.parseInt(line[1]);\n\n        Room room = new Room(N, M);\n\n        line = br.readLine().split(&quot; &quot;);\n\n        int r = Integer.parseInt(line[0]);\n        int c = Integer.parseInt(line[1]);\n        int d = Integer.parseInt(line[2]);\n\n        for (int i = 0; i &lt; N; i++) room.map[i] = Arrays.stream(br.readLine().split(&quot; &quot;)).mapToInt(Integer::parseInt).toArray();\n\n        System.out.println(room.cleanRoom(r, c, d));\n\n        br.close();\n    }\n}</code>\n        </deckgo-highlight-code>\n  \t</div>\n</details>\n<h3>✏️ <strong>Review</strong></h3>\n<ul>\n<li>시뮬레이션 문제는 주어진 조건에 따라 로직을 구현하는 방식으로 이해해, 로봇청소기의 작동원리마다의 메소드를 구현하려 했다.</li>\n<li>최대한 객체 지향적으로 들여쓰기 횟수도 고려하면서 메소드 추출을 수행했다.</li>\n</ul>\n<h3>📕 출처</h3>\n<p>Baekjoon : <a href=\"https://www.acmicpc.net/problem/14503\">https://www.acmicpc.net/problem/14503</a></p>","frontmatter":{"title":"👩‍💻 14503. 로봇 청소기","date":"March 15, 2022","update":"March 15, 2022","tags":["구현","시뮬레이션"],"series":"👩‍💻 BOJ"},"fields":{"slug":"/[BOJ]14503/","readingTime":{"minutes":6.29}}},"seriesList":{"edges":[{"node":{"id":"27733333-3aad-5771-a1a1-27b4de4b4a01","fields":{"slug":"/[BOJ]2225/"},"frontmatter":{"title":"👩‍💻 2225. 합분해"}}},{"node":{"id":"548ba188-2afe-5575-b79f-107c6c65b07d","fields":{"slug":"/[BOJ]1012/"},"frontmatter":{"title":"👩‍💻 1012. 유기농 배추"}}},{"node":{"id":"0c712680-130f-5fa5-8c3b-3632dd5d1af0","fields":{"slug":"/[BOJ]1260/"},"frontmatter":{"title":"👩‍💻 1260. DFS와 BFS"}}},{"node":{"id":"0e07a592-06ce-5e01-b5e1-95398330372e","fields":{"slug":"/[BOJ]1707/"},"frontmatter":{"title":"👩‍💻 1707. 이분그래프"}}},{"node":{"id":"df12bfec-ed5c-5fc4-8442-0a4b3f9759a3","fields":{"slug":"/[BOJ]2668/"},"frontmatter":{"title":"👩‍💻 2668. 숫자 고르기"}}},{"node":{"id":"01bbb6dc-812c-5223-87c5-53bf04c0d0a6","fields":{"slug":"/[BOJ]9466/"},"frontmatter":{"title":"👩‍💻 9466. 텀 프로젝트"}}},{"node":{"id":"abc306b3-c3a6-552a-8979-e82d506b3921","fields":{"slug":"/[BOJ]2146/"},"frontmatter":{"title":"👩‍💻 2146. 다리 만들기"}}},{"node":{"id":"b3394e53-5e9a-595e-8da4-bb3debd220c0","fields":{"slug":"/[BOJ]5014/"},"frontmatter":{"title":"👩‍💻 5014. 스타트 링크"}}},{"node":{"id":"be297703-e778-5727-8019-6954bc95da3a","fields":{"slug":"/[BOJ]11559/"},"frontmatter":{"title":"👩‍💻 11559. Puyo Puyo"}}},{"node":{"id":"ddf11545-d468-5269-94b1-5daa2720e886","fields":{"slug":"/[BOJ]17471/"},"frontmatter":{"title":"👩‍💻 17471. 게리맨더링"}}},{"node":{"id":"8d77114d-fc6e-5de2-a3ca-c3b250425a56","fields":{"slug":"/[BOJ]2437/"},"frontmatter":{"title":"👩‍💻 2437. 저울"}}},{"node":{"id":"9a8942ce-552f-51ae-b304-0d0fab8f2eba","fields":{"slug":"/[BOJ]14891/"},"frontmatter":{"title":"👩‍💻 14891. 톱니바퀴"}}},{"node":{"id":"f1c04e8a-df0a-5294-949f-5f3a483cbcb0","fields":{"slug":"/[BOJ]14503/"},"frontmatter":{"title":"👩‍💻 14503. 로봇 청소기"}}},{"node":{"id":"883b6ecf-7528-5c16-92fa-edf12a64e80e","fields":{"slug":"/[BOJ]3190/"},"frontmatter":{"title":"👩‍💻 3190. 뱀"}}},{"node":{"id":"099b2576-7d05-5872-a9f0-f1ee6efd3a9a","fields":{"slug":"/[BOJ]14502/"},"frontmatter":{"title":"👩‍💻 14502. 연구소"}}},{"node":{"id":"50eef059-4e55-5f14-a9cf-fb01b6506f02","fields":{"slug":"/[BOJ]17779/"},"frontmatter":{"title":"👩‍💻 17779. 게리맨더링 2"}}},{"node":{"id":"aa67d90b-4801-574c-b225-56c23737a5e2","fields":{"slug":"/[BOJ]2579/"},"frontmatter":{"title":"👩‍💻 2579. 계단 오르기"}}},{"node":{"id":"9db6b43b-963a-5ebd-b72b-69936cf37858","fields":{"slug":"/[BOJ]14499/"},"frontmatter":{"title":"👩‍💻 14499. 주사위 굴리기"}}},{"node":{"id":"6a79cddd-dc1b-5e34-8a84-af313c37de0e","fields":{"slug":"/[BOJ]15685/"},"frontmatter":{"title":"👩‍💻 15685. 드래곤 커브"}}},{"node":{"id":"5e001d34-d985-545b-83ec-7f1c7caea286","fields":{"slug":"/[BOJ]17144/"},"frontmatter":{"title":"👩‍💻 17144. 미세먼지 안녕!"}}},{"node":{"id":"1d132410-9377-5e55-85b9-4664ff64de73","fields":{"slug":"/[BOJ]9935/"},"frontmatter":{"title":"👩‍💻 9935. 문자열 폭발"}}},{"node":{"id":"e13ff8d6-9b02-56c0-83e2-1d3144f70e1d","fields":{"slug":"/[BOJ]16472/"},"frontmatter":{"title":"👩‍💻 16472. 고냥이"}}},{"node":{"id":"0f782160-aaa9-5c54-80c0-72a15172dd16","fields":{"slug":"/[BOJ]1083/"},"frontmatter":{"title":"👩‍💻 1083. 소트"}}},{"node":{"id":"fef58615-9e18-5f50-8d09-3d1e2839342f","fields":{"slug":"/[BOJ]20922/"},"frontmatter":{"title":"👩‍💻 20922. 겹치는 건 싫어"}}},{"node":{"id":"7ca89240-c65f-5cb4-aeef-c5f1605ebd00","fields":{"slug":"/[BOJ]16172/"},"frontmatter":{"title":"👩‍💻 16172. 나는 친구가 적다(Large)"}}}]},"previous":{"fields":{"slug":"/[JPA]5/"},"frontmatter":{"title":"📚 5. 연관관계 매핑 기초"}},"next":{"fields":{"slug":"/[BackEnd]Interceptor/"},"frontmatter":{"title":"💻 Interceptor"}}},"pageContext":{"id":"f1c04e8a-df0a-5294-949f-5f3a483cbcb0","series":"👩‍💻 BOJ","previousPostId":"c710307c-927c-5c1b-80fb-c45a4ff20616","nextPostId":"871dad9f-422b-50ed-8c98-d441dc97e4d0"}},"staticQueryHashes":[]}